﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Caifu.Fx;
using System.Text.RegularExpressions;
using Caifu.Clients;

//这里用域名做名称空间;
namespace user.caifu520.com
{

    public partial class _Default : PageBase
    {

        /// <summary>
        /// 提交表单的次数
        /// </summary>
        const Int16 PostMax = 2;

        /// <summary>
        /// 登录次数
        /// </summary>
        protected Int16 LoginCount
        {
            get
            {
                Int16 loginCount = 0;
                if (Session["logincount"] != null)
                {
                    Int16.TryParse(Session["logincount"].ToString(), out loginCount);
                }
                return loginCount;
            }
            set
            {
                Session["logincount"] = value;
            }
        }

        /// <summary>
        /// 登录次数
        /// </summary>
        protected Int16 RegisterCount
        {
            get
            {
                Int16 registerCount = 0;
                if (Session["registercount"] != null)
                {
                    Int16.TryParse(Session["registercount"].ToString(), out registerCount);
                }
                return registerCount;
            }
            set
            {
                Session["registercount"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
            {
                if (PV("hidaciton").Equals("0"))
                {
                    Login();
                }
                if (PV("hidaciton").Equals("1"))
                {
                    Register();
                }
            }
        }

        #region 注册流程：用户注册流程业务
        protected void Register()
        {
            RegisterCount++;
            if (LoginCount > PostMax)
            {
                this.CallScript("ChangeImg('imglogin',{pan:Page.tool.Dom.$('logImg'),display:'block'});");
            }
            if (RegisterCount > PostMax)
            {
                this.CallScript("ChangeImg('imgregister',{pan:Page.tool.Dom.$('rgstImg'),display:'block'});");
            }

            if (verifyEmail(this.PV("uname")) &&verifyPwd(this.PV("upwd"))
                && verifyPwd(this.PV("upwd"), this.PV("reupwd")))
            {
                if (!(RegisterCount > PostMax + 1 && verifyCode(this.PV("txtRgCode"))))
                {
                    alert("验证码错误");
                    return;
                }

                UserClient.Instance.AddUser(new Caifu.User()
                {
                    Email = PV("uname"),
                    Password = MD5Hash.ComputeHash(this.PV("upwd"))
                });
            }
        }


        #endregion

        #region 登录流程：用户登录业务流程
        protected void Login()
        {
            LoginCount++;
            if (RegisterCount > PostMax)
            {
                this.CallScript("ChangeImg('imgregister',{pan:Page.tool.Dom.$('rgstImg'),display:'block'});");
            }
            if (LoginCount > PostMax)
            {
                this.CallScript("ChangeImg('imglogin',{pan:Page.tool.Dom.$('logImg'),display:'block'});");
            }
        }
        #endregion

        /// <summary>
        /// 验证邮箱
        /// <param name="email">邮箱输入控件的名称</param>
        /// </summary>
        /// <returns>ture：通过，false：未通过</returns>
        Boolean verifyEmail(String email)
        {
            Regex rx = new Regex(@"^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$");
            if (email.Equals(""))
            {
                alert("请输入邮箱地址");
                return false;
            }
            if (!rx.IsMatch(email))
            {
                alert("邮箱地址格式不正确");
                return false;
            }
            if (CheckExistByEmail(email))
            {
                alert("该邮箱已被注册，请更换其他邮箱");
                return false;
            }
            if (email.Length > 50)
            {
                alert("邮箱地址不得超过50个字符");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 注册时验证用户密码
        /// <param name="pwd">密码输入控件的名称</param>
        /// <param name="repwd">确认密码控件的名称</param>
        /// </summary>
        /// <returns>ture：通过，false：未通过</returns>
        Boolean verifyPwd(String pwd, String repwd)
        {
            Regex rx = new Regex(@"^[0-9a-zA-Z][0-9a-zA-Z]*?$");
            if (repwd.Equals(""))
            {
                alert("请输入确认密码");
                return false;
            }
            if (!repwd.Equals(pwd))
            {
                alert("两次密码输入不一致");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 登录时验证输入用户密码
        /// </summary>
        /// <returns>ture：通过，false：未通过</returns>
        Boolean verifyPwd(String pwd)
        {
            Regex rx = new Regex(@"^[0-9a-zA-Z][0-9a-zA-Z]*?$");
            if (pwd.Equals(""))
            {
                alert("密码不能为空");
                return false;
            }
            if (!rx.IsMatch(pwd))
            {
                alert("密码中只能包含字母和数字");
                return false;
            }
            if (pwd.Length < 6 || pwd.Length > 16)
            {
                alert("密码的长度为：6—16个字符");
                return false;
            }
            return true;
        }




        /// <summary>
        /// 检查用户邮箱的存在性；
        /// </summary>
        /// <param name="email">用户邮箱</param>
        /// <returns>true:用户存在，false:用户不存在</returns>
        Boolean CheckExistByEmail(String email)
        {
            return UserClient.Instance.CheckExistByEmail(email);
        }


        /// <summary>
        /// 检测用户昵称的存在性;
        /// </summary>
        /// <returns>true:存在冲突，false:不存在冲突</returns>
        Boolean CheckExistByName(String username)
        {
            return UserClient.Instance.CheckExistByName(username);
        }

        /// <summary>
        /// 验证用户输入的验证码信息
        /// </summary>
        /// <param name="inputCode">用户输入的验证码</param>
        /// <returns></returns>
        Boolean verifyCode(String inputCode)
        {
            return (new ValidateCodeManager(this.Context)).ValidationCode(inputCode);
        }
    }
}